﻿namespace NH3
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using FluentNHibernate;
	using FluentNHibernate.Conventions;
	using FluentNHibernate.MappingModel.Identity;

	public class MyForeignKeyConvention : ForeignKeyConvention
	{
		private static readonly IList<IMappingProvider> Mappings = new List<IMappingProvider>();
	
		protected override string GetKeyName(Member property, Type type)
		{
			var pk = "Id";

			var model = new PersistenceModel();
			foreach (var map in Mappings)
			{
				model.Add(map);
			}

			try
			{
				var mymodel = (IdMapping)model.BuildMappings()
					.First(x => x.Classes.FirstOrDefault(c => c.Type == type) != null)
					.Classes.First().Id;

				Func<IdMapping, string> getname = x => x.Columns.First().Name;
				pk = getname(mymodel);
			}
			catch
			{
			}

			if (property == null)
			{
				return pk;
			}

			return type.Name + property.Name;
		}
	}
}
